<template>
  <div class="ele-body2">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form style="padding-bottom: 5px"
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="发票号:">
              <el-input
                clearable
                v-model="where.vinvoicecode"
                placeholder=""/>
            </el-form-item>
          </el-col>


          <el-col :lg="6" :md="12">
            <el-form-item label="供应商:">
              <el-input
                clearable
                v-model="where.custname"
                placeholder=""/>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="存货编号:">
              <el-input
                clearable
                v-model="where.invcode"
                placeholder=""/>
            </el-form-item>

          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="批号:" label-width="50px">
              <el-input
                clearable
                v-model="where.batch"
                placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :lg="3" :md="12">
            <el-form-item label="二票取消:">
              <el-select
                clearable
                v-model="where.isclose">
                <el-option label="是" value="1"/>
                <el-option label="否" value="0"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="3" :md="12">
            <el-form-item label="状态:" label-width="50px">
              <el-select
                clearable
                v-model="where.status">
                <el-option label="同步" value="0"/>
                <el-option label="上报" value="1"/>
                <el-option label="上报确认" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="3" :md="12">
            <el-form-item label="是否二票:">
              <el-select
                clearable
                v-model="where.isposo">
                <el-option label="是" value="1"/>
                <el-option label="否" value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="5" :sm="12">
            <el-form-item label="发票日期:">
              <el-date-picker v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="5" :sm="12">
            <el-form-item label="结束日期:">
              <el-date-picker v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="12">
            <div class="ele-form-actions" style="padding-top: 4px">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="mini"
                @click="reload">查询数据
              </el-button>
              <el-button @click="reset" size="mini">重置</el-button>
            </div>
          </el-col>

        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        :toolbar="true"
        :pageSize="100"
        :header-cell-style="{'text-align':'center'}"
        highlight-current-row
        @row-dblclick="reset"
        height="calc(100vh - 255px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="ele-btn-icon"
            size="small"
            @click="invoiceUpload">发票传报
          </el-button>
          <el-button
            type="warning"
            icon="el-icon-search"
            class="ele-btn-icon"
            size="small"
            @click="invoiceConfirm">发票确认
          </el-button>
        </template>
        <template slot="fph" slot-scope="{row}">
          <a href="#" @click="openEdit(row)">{{row.vinvoicecode}}</a>
        </template>
        <!-- 操作列 -->
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload2"
            @click="openEdit(row)"
          >NC回写
          </el-link>
          <el-link
            type="success"
            slot="reference"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志
          </el-link>
          <el-link v-if="false"
                   type="primary"
                   :underline="false"
                   icon="el-icon-search"
                   @click="openDetail(row)"
          >单据明细
          </el-link>

        </template>
      </ele-pro-table>
    </el-card>
    <!-- <LogList
    :dataItem="current"
    :visible.sync="showLog"
    />
    <BillDetail
    :dataItem="current"
    :visible.sync="showDetail"
    /> -->

    <!-- 编辑弹窗 -->
    <InvocieDetail
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import InvocieDetail from './invoice-detail';
//   import LogList from '../../purchase/inbound/loglist.vue';
//   import BillDetail from '../ordersync/billdetail.vue';
// import C37InfCkScBillNewEdit from './c37infckscbillnew-edit';

export default {
  name: 'ygorder_purchinvoicesync',
  // components: {C37InfCkScBillNewEdit},
  components: {InvocieDetail},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/dinvoiceinfo/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 50,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'vinvoicedm',
          label: '发票代码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'vinvoicecode',
          label: '发票号',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot: 'fph'
        },
        {
          prop: 'dinvoicedate',
          label: '发票日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        // {
        //   prop: 'cupsourcebillcode',
        //   label: '出库单号',
        //   showOverflowTooltip: true,
        //   minWidth: 140,
        //   align: 'center'
        // },
        {
          prop: 'custcode',
          label: '供应商编码',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'custname',
          label: '供应商名称',
          showOverflowTooltip: true,
          minWidth: 260,
          align: 'left'
        },
        {
          prop: 'fileupLoadflg',
          label: '文件下载',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'statusdesc',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'iscloseDesc',
          label: '是否取消',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'retmemo',
          label: '处理结果',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        //   {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 160,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      // 表格搜索条件
      where: {riqiBegin:this.getFromDate(),riqiEnd:new Date(),soucetype:'阳光平台',czlx:'药事新增',uploadPsd:true,uploadFp:true,isposo:'0',status:'0',ispage:'1',isclose:'0'},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail:false,
    };
  },
  methods: {
    statusFormat(row, column,cellValue) {
      const daterc = row[column.property]
      if(daterc !=null){
        if(cellValue=="0") {
          return "同步";
        }else {
          return cellValue == "2" ? "上报确认" : "已上报";
        }
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth();
      let day = timeOne.getDate()-5;
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date()};
      this.reload();
    },

    openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 发票传报 */
    invoiceUpload() {
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要上传至阳光平台的发票。');
        return;
      }
      if(this.selection[0].status!='0')
      {
        this.$message.info('进项发票已传报，无需再次传报。');
        return;
      }
      this.$confirm('确定要将选中的发票上传至阳光平台吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/yq029', {ddmxbhs:this.selection.map(d => d.cinvoiceid)}).then(res => {
          loading.close();
          this.reload();
          if (Number(res.data.code) === 0) {
            this.$message.success(res.data.msg);
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 发票确认 */
    invoiceConfirm() {
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要确认的发票。');
        return;
      }
      if(this.selection[0].status=='0')
      {
        this.$message.info('进货发票未传报，请先传报后再确认。');
        return;
      }
      if(this.selection[0].status=='2')
      {
        this.$message.info('进项发票已确认，无需再次确认。');
        return;
      }

      this.$confirm('确定要将选中的发票进行确认吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/yq030', {ddmxbhs:this.selection.map(d => d.cinvoiceid)}).then(res => {
          loading.close();
          this.reload();
          if (Number(res.data.code) === 0) {
            this.$message.success(res.data.msg);
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的进项发票吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/mpruchinvoice/delete/'+this.selection.map(d => d.id)).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
</style>
